﻿/*
@class: Rectangle Class
@author: Darren D. Benston
@created: 24 jun 2007
@last modified: 
*/
/*
// CHANGE LOG
mod ## jun ## - ddb
	ver 1.00
	created:	created Rectangle class
//
*/
/*
// METHOD TABLE:
// Rectangle methods
//
function Rectangle() {
public function create(p_name_str:String, p_depth_num:Number, p_scope_mc:MovieClip, p_props_obj:Object):MovieClip {
private function toString():Void{
*/
/*
	**NOTE** strokeWidth_num:Number
		if the parameter is not specified (implicitly undefined),
		or if the parameter is undefined, (expicitly - ie: strokeWidth_num = undefined)
		or if the parameter has a value of less than 0 (negative value),
		or if the parameter is null (explicitly - ie: strokeWidth_num = null),
			a stroke is not drawn.
		if the parameter has a value of 0 (explicitly),
			a hairline thickness will be drawn
*/

class com.utils.Rectangle {
	// properties
	private var className_str:String;
	private var version_str:String;
	// constructor
	function Rectangle() {
		className_str = "  Rectangle";
		version_str = "v1.00";
		//trace(this);
		// empty
	}
	public static function create(p_name_str:String, p_depth_num:Number, p_scope_mc:MovieClip, p_props_obj:Object):MovieClip {
		//trace("Rectangle.create()");
		//trace("  p_name_str:"+p_name_str);
		//trace("  p_depth_num:"+p_depth_num);
		//trace("  p_scope_mc:"+p_scope_mc);
		//trace("  p_props_obj:"+p_props_obj);
		var scope_mc:MovieClip;
		//
		var depth_num:Number;
		var name_str:String;
		var width_num:Number;
		var height_num:Number;
		var cornerRadius_arr:Array;
		var fillColor_num:Number;
		var strokeColor_arr:Array;
		var strokeWidth_num:Number;
		//
		var default_depth_num:Number = p_scope_mc.getNextHighestDepth();
		var default_id_num:Number = new Date().getTime()+Math.random();
		var default_name_str:String = "rectangle"+default_id_num+"_mc";
		var default_width_num:Number = 200;
		var default_height_num:Number = 100;
		var default_cornerRadius_arr:Array = new Array(0,0,0,0);
		var default_fillColor_num:Number = 0xEEEEEE;
		var default_strokeColor_arr:Array = new Array(0xCCCCCC, 0x888888, 0x888888, 0xCCCCCC);
		//
		var rectangle_mc:MovieClip;
		// required parameters
		// fail if undefined
		if (p_scope_mc == null || p_scope_mc == undefined) {
			trace("    error: scope_mc is undefined");
			return null;
		}
		// optional parameters
		// set defaults if undefined
		if (p_depth_num == null || p_depth_num == undefined) {
			p_depth_num = default_depth_num;
		}
		if (p_name_str == null || p_name_str == undefined) {
			p_name_str = default_name_str;
		}
		if (p_props_obj == null || p_props_obj == undefined) {
			trace("    props_obj is undefined");
			p_props_obj = new Object();
		}
		if (p_props_obj.width_num == null || p_props_obj.width_num == undefined) {
			p_props_obj.width_num = default_width_num;
		}
		if (p_props_obj.height_num == null || p_props_obj.height_num == undefined) {
			p_props_obj.height_num = default_height_num;
		}
		if (p_props_obj.cornerRadius_arr == null || p_props_obj.cornerRadius_arr == undefined) {
			p_props_obj.cornerRadius_arr = default_cornerRadius_arr;
		}
		if (p_props_obj.fillColor_num == null || p_props_obj.fillColor_num == undefined) {
			p_props_obj.fillColor_num = default_fillColor_num;
		}
		if (p_props_obj.strokeColor_arr == null || p_props_obj.strokeColor_arr == undefined) {
			// check for strokeColor_num
			if (p_props_obj.strokeColor_num == null || p_props_obj.strokeColor_num == undefined) {
				// use default_strokeColor_arr
				p_props_obj.strokeColor_arr = default_strokeColor_arr;
			} else {
				// use strokeColor_num 
				p_props_obj.strokeColor_arr = new Array(p_props_obj.strokeColor_num, p_props_obj.strokeColor_num, p_props_obj.strokeColor_num, p_props_obj.strokeColor_num);
			}
		}
		/*
			**NOTE** strokeWidth_num:Number
				if the parameter is not specified (implicitly undefined),
				or if the parameter is undefined, (expicitly - ie: strokeWidth_num = undefined)
				or if the parameter has a value of less than 0 (negative value),
				or if the parameter is null (explicitly - ie: strokeWidth_num = null),
					a stroke is not drawn.
				if the parameter has a value of 0 (explicitly),
					a hairline thickness will be drawn
		*/
		// assign properties from paramaters 
		scope_mc = p_scope_mc;
		depth_num = p_depth_num;
		name_str = p_name_str;
		// 
		rectangle_mc = scope_mc.createEmptyMovieClip(name_str, depth_num);
		//
		cornerRadius_arr = p_props_obj.cornerRadius_arr;
		fillColor_num = p_props_obj.fillColor_num;
		strokeColor_arr = p_props_obj.strokeColor_arr;
		strokeWidth_num = p_props_obj.strokeWidth_num;
		//
		height_num = p_props_obj.height_num;
		width_num = p_props_obj.width_num;
		if (strokeWidth_num === 0) {
			// hairline stroke
		} else if (strokeWidth_num === null || strokeWidth_num === undefined || strokeWidth_num < 0) {
			// none stroke
			strokeWidth_num = undefined;
		} else {
			// stroke
			// subtract stroke from width
			width_num = width_num - strokeWidth_num;
			height_num = height_num - strokeWidth_num;
		}
		/*
		trace("  scope_mc:"+scope_mc);
		trace("  depth_num:"+depth_num);
		trace("  name_str:"+name_str);
		trace("  width_num:"+width_num);
		trace("  height_num:"+height_num);
		trace("  rectangle_mc.width_num:"+rectangle_mc.width_num);
		trace("  rectangle_mc.height_num:"+rectangle_mc.height_num);
		trace("  cornerRadius_arr:"+cornerRadius_arr);
		trace("  fillColor_num:"+fillColor_num);
		trace("  strokeColor_arr:"+strokeColor_arr);
		trace("  strokeWidth_num:"+strokeWidth_num);
		*/
		rectangle_mc.suicide = function(){
			removeMovieClip(this);
		};
		// set array index's 
		var T:Number = 0;
		var R:Number = 1;
		var B:Number = 2;
		var L:Number = 3;
		var theta_num:Number;
		var angle_num:Number;
		var cx:Number;
		var cy:Number;
		var px:Number;
		var py:Number;
		var x:Number = 0;
		var y:Number = 0;
		var w:Number = width_num;
		var h:Number = height_num;
		var a:Number = 100;
		var pH_bool:Boolean = false;
		var noScale_str:String = "none";
		var capStyle_str:String;
		var jointStyle_str:String
		var angleCapStyle_str:String = "square";
		var angleJointStyle_str:String = undefined;;
		var curveCapStyle_str:String = "round";
		var curveJointStyle_str:String = "round";;
		// theta_num = 45 degrees in radians
		theta_num = Math.PI/4;
		//angle_num is currently 90 degrees
		angle_num = -Math.PI/2;
		// set fill
		rectangle_mc.beginFill(fillColor_num);
		// draw top line
		if (cornerRadius_arr[T] == 0) {
			capStyle_str = angleCapStyle_str;
			jointStyle_str = angleJointStyle_str;
		} else {
			capStyle_str = curveCapStyle_str;
			jointStyle_str = curveJointStyle_str;
		}
		rectangle_mc.lineStyle(strokeWidth_num, strokeColor_arr[T], a, pH_bool, noScale_str, capStyle_str, jointStyle_str);
		rectangle_mc.moveTo(x+cornerRadius_arr[T], y);
		rectangle_mc.lineTo(x+w-cornerRadius_arr[T], y);
		// draw tr corner in two parts
		// T
		cx = x+w-cornerRadius_arr[T]+(Math.cos(angle_num+(theta_num/2))*cornerRadius_arr[T]/Math.cos(theta_num/2));
		cy = y+cornerRadius_arr[T]+(Math.sin(angle_num+(theta_num/2))*cornerRadius_arr[T]/Math.cos(theta_num/2));
		px = x+w-cornerRadius_arr[T]+(Math.cos(angle_num+theta_num)*cornerRadius_arr[T]);
		py = y+cornerRadius_arr[T]+(Math.sin(angle_num+theta_num)*cornerRadius_arr[T]);
		rectangle_mc.curveTo(cx, cy, px, py);
		// R
		if (cornerRadius_arr[R] == 0) {
			capStyle_str = angleCapStyle_str;
			jointStyle_str = angleJointStyle_str;
		} else {
			capStyle_str = curveCapStyle_str;
			jointStyle_str = curveJointStyle_str;
		}
		rectangle_mc.lineStyle(strokeWidth_num, strokeColor_arr[R], a, pH_bool, noScale_str, capStyle_str, jointStyle_str);
		angle_num += theta_num;
		cx = x+w-cornerRadius_arr[R]+(Math.cos(angle_num+(theta_num/2))*cornerRadius_arr[R]/Math.cos(theta_num/2));
		cy = y+cornerRadius_arr[R]+(Math.sin(angle_num+(theta_num/2))*cornerRadius_arr[R]/Math.cos(theta_num/2));
		px = x+w-cornerRadius_arr[R]+(Math.cos(angle_num+theta_num)*cornerRadius_arr[R]);
		py = y+cornerRadius_arr[R]+(Math.sin(angle_num+theta_num)*cornerRadius_arr[R]);
		rectangle_mc.curveTo(cx, cy, px, py);
		// draw right line
		rectangle_mc.lineTo(x+w, y+h-cornerRadius_arr[R]);
		// draw br corner
		// R
		angle_num += theta_num;
		cx = x+w-cornerRadius_arr[R]+(Math.cos(angle_num+(theta_num/2))*cornerRadius_arr[R]/Math.cos(theta_num/2));
		cy = y+h-cornerRadius_arr[R]+(Math.sin(angle_num+(theta_num/2))*cornerRadius_arr[R]/Math.cos(theta_num/2));
		px = x+w-cornerRadius_arr[R]+(Math.cos(angle_num+theta_num)*cornerRadius_arr[R]);
		py = y+h-cornerRadius_arr[R]+(Math.sin(angle_num+theta_num)*cornerRadius_arr[R]);
		rectangle_mc.curveTo(cx, cy, px, py);
		// B
		if (cornerRadius_arr[B] == 0) {
			capStyle_str = angleCapStyle_str;
			jointStyle_str = angleJointStyle_str;
		} else {
			capStyle_str = curveCapStyle_str;
			jointStyle_str = curveJointStyle_str;
		}
		rectangle_mc.lineStyle(strokeWidth_num, strokeColor_arr[B], a, pH_bool, noScale_str, capStyle_str, jointStyle_str);
		angle_num += theta_num;
		cx = x+w-cornerRadius_arr[B]+(Math.cos(angle_num+(theta_num/2))*cornerRadius_arr[B]/Math.cos(theta_num/2));
		cy = y+h-cornerRadius_arr[B]+(Math.sin(angle_num+(theta_num/2))*cornerRadius_arr[B]/Math.cos(theta_num/2));
		px = x+w-cornerRadius_arr[B]+(Math.cos(angle_num+theta_num)*cornerRadius_arr[B]);
		py = y+h-cornerRadius_arr[B]+(Math.sin(angle_num+theta_num)*cornerRadius_arr[B]);
		rectangle_mc.curveTo(cx, cy, px, py);
		// draw bottom line
		rectangle_mc.lineTo(x+cornerRadius_arr[B], y+h);
		// draw bl corner
		// B
		angle_num += theta_num;
		cx = x+cornerRadius_arr[B]+(Math.cos(angle_num+(theta_num/2))*cornerRadius_arr[B]/Math.cos(theta_num/2));
		cy = y+h-cornerRadius_arr[B]+(Math.sin(angle_num+(theta_num/2))*cornerRadius_arr[B]/Math.cos(theta_num/2));
		px = x+cornerRadius_arr[B]+(Math.cos(angle_num+theta_num)*cornerRadius_arr[B]);
		py = y+h-cornerRadius_arr[B]+(Math.sin(angle_num+theta_num)*cornerRadius_arr[B]);
		rectangle_mc.curveTo(cx, cy, px, py);
		// L
		if (cornerRadius_arr[L] == 0) {
			capStyle_str = angleCapStyle_str;
			jointStyle_str = angleJointStyle_str;
		} else {
			capStyle_str = curveCapStyle_str;
			jointStyle_str = curveJointStyle_str;
		}
		rectangle_mc.lineStyle(strokeWidth_num, strokeColor_arr[L], a, pH_bool, noScale_str, capStyle_str, jointStyle_str);
		angle_num += theta_num;
		cx = x+cornerRadius_arr[L]+(Math.cos(angle_num+(theta_num/2))*cornerRadius_arr[L]/Math.cos(theta_num/2));
		cy = y+h-cornerRadius_arr[L]+(Math.sin(angle_num+(theta_num/2))*cornerRadius_arr[L]/Math.cos(theta_num/2));
		px = x+cornerRadius_arr[L]+(Math.cos(angle_num+theta_num)*cornerRadius_arr[L]);
		py = y+h-cornerRadius_arr[L]+(Math.sin(angle_num+theta_num)*cornerRadius_arr[L]);
		rectangle_mc.curveTo(cx, cy, px, py);
		// draw left line
		rectangle_mc.lineTo(x, y+cornerRadius_arr[L]);
		// draw tl corner
		// L
		angle_num += theta_num;
		cx = x+cornerRadius_arr[L]+(Math.cos(angle_num+(theta_num/2))*cornerRadius_arr[L]/Math.cos(theta_num/2));
		cy = y+cornerRadius_arr[L]+(Math.sin(angle_num+(theta_num/2))*cornerRadius_arr[L]/Math.cos(theta_num/2));
		px = x+cornerRadius_arr[L]+(Math.cos(angle_num+theta_num)*cornerRadius_arr[L]);
		py = y+cornerRadius_arr[L]+(Math.sin(angle_num+theta_num)*cornerRadius_arr[L]);
		rectangle_mc.curveTo(cx, cy, px, py);
		// T
		if (cornerRadius_arr[T] == 0) {
			capStyle_str = angleCapStyle_str;
			jointStyle_str = angleJointStyle_str;
		} else {
			capStyle_str = curveCapStyle_str;
			jointStyle_str = curveJointStyle_str;
		}
		rectangle_mc.lineStyle(strokeWidth_num, strokeColor_arr[T], a, pH_bool, noScale_str, capStyle_str, jointStyle_str);
		angle_num += theta_num;
		cx = x+cornerRadius_arr[T]+(Math.cos(angle_num+(theta_num/2))*cornerRadius_arr[T]/Math.cos(theta_num/2));
		cy = y+cornerRadius_arr[T]+(Math.sin(angle_num+(theta_num/2))*cornerRadius_arr[T]/Math.cos(theta_num/2));
		px = x+cornerRadius_arr[T]+(Math.cos(angle_num+theta_num)*cornerRadius_arr[T]);
		py = y+cornerRadius_arr[T]+(Math.sin(angle_num+theta_num)*cornerRadius_arr[T]);
		rectangle_mc.curveTo(cx, cy, px, py);
		rectangle_mc.endFill();
		// return reference to mc
		return rectangle_mc;
	}
	private function toString():String {
		return className_str + " ("+version_str+")";
	}
}
